package com.gem.service.impl;


import com.gem.domain.ClassInfo;
import com.gem.domain.ClassInfoAPI;
import com.gem.mapper.ClassInfoMapper;
import com.gem.service.ClassInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;

@Service
@Transactional(rollbackFor=Exception.class)
public class ClassInfoServiceImpl implements ClassInfoService {

    @Autowired
    private ClassInfoMapper mapper;


    @Override
    public PageInfo<ClassInfo> findAll(int currentPage, int pageSize) {
        PageHelper.startPage(currentPage, pageSize);
        return new PageInfo<ClassInfo>(mapper.findAll());
    }

    @Override
    public PageInfo<ClassInfo> searchClassInfo(ClassInfo ClassInfo, int currentPage, int pageSize) {
        //日期搜索
        if (ClassInfo.getStartTime() != null) {
            if ("null".equals(ClassInfo.getStartTime()) ) {
                ClassInfo.setStartTime(null);
            }
        }
        //讲师搜索
        if (ClassInfo.getTName() != null) {
            if ("null".equals(ClassInfo.getTName()) || ClassInfo.getTName().length() == 0) {
                ClassInfo.setTName(null);
            } else {
                ClassInfo.setTName(ClassInfo.getTName().replaceAll(" ", ""));
                ClassInfo.setTName("%" + ClassInfo.getTName() + "%");
            }
        }
        //班主任搜索
        if (ClassInfo.getMName() != null) {
            if ("null".equals(ClassInfo.getMName()) || ClassInfo.getMName().length() == 0 ) {
                ClassInfo.setMName(null);
            } else {
                ClassInfo.setMName(ClassInfo.getMName().replaceAll(" ", ""));
                ClassInfo.setMName("%" + ClassInfo.getMName() + "%");
            }
        }
        //类型搜索
        if (ClassInfo.getSName() != null) {
            if ("null".equals(ClassInfo.getSName()) || ClassInfo.getSName().length() == 0  ) {
                ClassInfo.setSName(null);
            } else {
                ClassInfo.setSName(ClassInfo.getSName().replaceAll(" ", ""));
                ClassInfo.setSName("%" + ClassInfo.getSName() + "%");
            }
        }
        PageInfo<ClassInfo> pageInfo = null;
        try {
            PageHelper.startPage(currentPage, pageSize);
            pageInfo = new PageInfo<ClassInfo>(mapper.searchClassInfo(ClassInfo));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return pageInfo;
    }

    @Override
    public boolean deleteById(Integer classId) {
        try {
           if (mapper.deleteById(classId)){
               return true;
           }else {
               return false;
           }
        } catch (Exception e) {
            e.printStackTrace();
            return false;

        }
    }

    @Override
    public boolean save(ClassInfoAPI ClassInfo) {
        try {
            if ( mapper.save(ClassInfo)){
                return true;
            }else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean update(ClassInfoAPI ClassInfo) {
        try {
            if ( mapper.update(ClassInfo)){
                return true;
            }else {
                return false;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public ClassInfo findById(int id) {
        return mapper.findById(id);
    }

    @Override
    public List<ClassInfo> findAllClass() {
        return mapper.findAll();
    }
}
